package com.sec.android.app.myfiles.listener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;

import com.sec.android.app.myfiles.listener.ListenerMgr.LifeCycle;
import com.sec.android.app.myfiles.log.Log;

public class SystemBroadcastReceiver extends AbsBroadcastReceiverImp {
    public interface OnMediaMounted {
        void onMediaMounted(String path);
    }

    public interface OnMediaUnmounted {
        void onMediaUnmounted(String path);
    }

    public interface OnMediaEject {
        void onMediaEject(String path);
    }

    public interface OnMediaScanStart {
        void onMediaScanStart();
    }

    public interface OnMediaScanFinish {
        void onMediaScanFinish();
    }

    public interface OnLocaleChanged {
        void onLocaleChanged();
    }

    private OnMediaMounted mMediaMountedListener;
    private OnMediaUnmounted mMediaUnmountedListener;
    private OnMediaEject mMediaEjectListener;
    private OnMediaScanStart mMediaScanStartListener;
    private OnMediaScanFinish mMediaScanFinishListener;
    private OnLocaleChanged mLocaleChangedListener;

    public SystemBroadcastReceiver(Context context, Object listener, LifeCycle registerTime, LifeCycle unregisterTime) {
        super(context, registerTime, unregisterTime);
        addListener(listener);
    }

    @Override
    IntentFilter getIntentFilter() {
        IntentFilter ret = new IntentFilter();
        ret.addAction(Intent.ACTION_MEDIA_MOUNTED);
        ret.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
        ret.addAction(Intent.ACTION_MEDIA_EJECT);
        ret.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
        ret.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
        ret.addAction(Intent.ACTION_LOCALE_CHANGED);

        ret.addDataScheme("file");

        return ret;
    }

    @Override
    BroadcastReceiver getBroadcastReceiver() {
        BroadcastReceiver ret = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Uri uri = intent.getData();
                String path = null;
                if (uri != null) {
                    path = uri.getPath();
                }
                Log.d(this, "onReceive() action : " + intent.getAction() + " path : " + path);

                if (Intent.ACTION_MEDIA_MOUNTED.equals(intent.getAction())) {
                    if (mMediaMountedListener != null) {
                        mMediaMountedListener.onMediaMounted(path);
                    }
                } else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(intent.getAction())) {
                    if (mMediaUnmountedListener != null) {
                        mMediaUnmountedListener.onMediaUnmounted(path);
                    }
                } else if (Intent.ACTION_MEDIA_EJECT.equals(intent.getAction())) {
                    if (mMediaEjectListener != null) {
                        mMediaEjectListener.onMediaEject(path);
                    }
                } else if (Intent.ACTION_MEDIA_SCANNER_STARTED.equals(intent.getAction())) {
                    if (mMediaScanStartListener != null) {
                        mMediaScanStartListener.onMediaScanStart();
                    }
                } else if (Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(intent.getAction())) {
                    if (mMediaScanFinishListener != null) {
                        mMediaScanFinishListener.onMediaScanFinish();
                    }
                } else if (Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())) {
                    if (mLocaleChangedListener != null) {
                        mLocaleChangedListener.onLocaleChanged();
                    }
                }
            }
        };

        return ret;
    }

    private void addListener(Object listener) {
        if (listener instanceof OnMediaMounted) {
            mMediaMountedListener = (OnMediaMounted) listener;
        }
        if (listener instanceof OnMediaUnmounted) {
            mMediaUnmountedListener = (OnMediaUnmounted) listener;
        }
        if (listener instanceof OnMediaEject) {
            mMediaEjectListener = (OnMediaEject) listener;
        }
        if (listener instanceof OnMediaScanStart) {
            mMediaScanStartListener = (OnMediaScanStart) listener;
        }
        if (listener instanceof OnMediaScanFinish) {
            mMediaScanFinishListener = (OnMediaScanFinish) listener;
        }
        if (listener instanceof OnLocaleChanged) {
            mLocaleChangedListener = (OnLocaleChanged) listener;
        }
    }

}
